cmake_minimum_required(VERSION 3.4)

project(lpk)
include_directories(els stdlib)

# Compiler Options
set(CMAKE_CXX_STANDARD 14)

if (MSVC)
    set(CMAKE_C_FLAGS "/O2 /EHsc /utf-8 /w")
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
    set(rc_flags "/nologo /c65001")
    set(CMAKE_RC_FLAGS ${rc_flags})
elseif (CMAKE_COMPILER_IS_GNUCXX)
    if (WIN32)
        set(CMAKE_C_FLAGS "-std=c89 -fPIC -O2 -finput-charset=utf-8 -fexec-charset=gbk -DPLATLINUX -DLANGENG -DLANGCN")
    else ()
        set(CMAKE_C_FLAGS "-std=c89 -fPIC -O2 -DPLATLINUX -DLANGENG -DLANGCN")
    endif ()
    set(CMAKE_CXX_FLAGS "-std=c++11 -fPIC -O2 -DPLATLINUX")
else ()
    set(CMAKE_C_FLAGS "-std=c89 -fPIC -O2 -DPLATLINUX -DLANGENG -DLANGCN -w")
    set(CMAKE_CXX_FLAGS "-std=c++11 -fPIC -O2 -DPLATLINUX -w")
endif ()

# Source Code
set(ELS_SOURCE_CODE
        els/catch.c
        els/els_c_api.c
        els/els_vm_bytecode.c
        els/els_vm.c
        els/els.c
        els/func.c
        els/hash.c
        els/inout.c
        els/lex.c
        els/parser.c
        els/unit.c)

set(STDLIB_SOURCE_CODE
        stdlib/clib/Losu_Std_File.cpp
        stdlib/clib/Losu_Std_Math.cpp
        stdlib/clib/Losu_Std_Md5.cpp
        stdlib/clib/Losu_Std_Os.cpp
        stdlib/clib/Losu_Std_Std.cpp
        stdlib/clib/Losu_Std_String.cpp
        stdlib/clib/Losu_Std_Time.cpp)

add_executable(elf ${ELS_SOURCE_CODE})
add_library(stdlib SHARED ${STDLIB_SOURCE_CODE})

set_target_properties(stdlib PROPERTIES OUTPUT_NAME libstd)
set_target_properties(stdlib PROPERTIES PREFIX "")
set_target_properties(stdlib PROPERTIES SUFFIX ".lsd")

if (APPLE)
    add_executable(elf_arm ${ELS_SOURCE_CODE})
    add_library(stdlib_arm SHARED ${STDLIB_SOURCE_CODE})

    set_target_properties(stdlib_arm PROPERTIES OUTPUT_NAME libstd_arm)
    set_target_properties(stdlib_arm PROPERTIES PREFIX "")
    set_target_properties(stdlib_arm PROPERTIES SUFFIX ".lsd")

    # Set for x86_64 target
    target_compile_options(elf PUBLIC -target x86_64-apple-macos10.12)
    target_link_options(elf PUBLIC -target x86_64-apple-macos10.12)
    target_compile_options(stdlib PUBLIC -target x86_64-apple-macos10.12)
    target_link_options(stdlib PUBLIC -target x86_64-apple-macos10.12)

    # Set for ARM64 target
    target_compile_options(elf_arm PUBLIC -target arm64-apple-macos11)
    target_link_options(elf_arm PUBLIC -target arm64-apple-macos11)
    target_compile_options(stdlib_arm PUBLIC -target arm64-apple-macos11)
    target_link_options(stdlib_arm PUBLIC -target arm64-apple-macos11)
endif ()
